<?php 
	require_once("MoneyFactory.class.php"); 
	require_once("MoneyOps.class.php"); 

	function processRequest($_REQUEST) {

			if (isset($_REQUEST["ops"]) && 
				isset($_REQUEST["v1"]) && isset($_REQUEST["c1"])
			) {
				$opsCode=$_REQUEST["ops"];
				echo "<div id='result'>Resultat de l'opération<div id='ops'>$opsCode</div> est : </div>";
				echo "<div id='result_detail'>";
				
				if (strcmp($opsCode,"ADD")==0) {
					$res=MoneyOps::add($m1,$m2);
				} 
				
				if (isset($res) && ($res instanceOf Money)) {
                  setcookie('PorteMonnaie',$res->getValue(),time()+3600);
				  echo "(",$res->getCurrency(),") ",$res->getValue();
				} else echo "Unsupported operations"; 
				echo "</div>";
            }
                if(!isset($_COOKIE['PorteMonnaie'])){
                    setcookie('PorteMonnaie',20,time()+3600);
                }
                echo "Votre solde est :".$_COOKIE['PorteMonnaie'];
	}
?>
<html>
	<head>
		<title>Money Operations</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	</head>
	<body>
		<?php
		processRequest($_REQUEST);	
		?>
		<hr width='20%' align='left'/>
		<form action='index.php' method='post' id='form0'>
		<table>
			<tr>
			  <td>Valeur</td><td>Devise</td><td>Opération</td>
			</tr>
			<tr>
			  <td><input type='text' id='v1' name='v1' value='<?php if(isset($_REQUEST["v1"]))echo $_REQUEST["v1"];?>'/></td>
			  <td><input type='text' id='c1' name='c1' value='<?php if(isset($_REQUEST["c1"]))echo $_REQUEST["c1"];?>'/></td>
			  <td><select name='ops'><option value='ADD'>+</option></select></td>
			</tr>
			<tr>
			   <td colspan='5' align='right'><input type='submit' value='calculer'/></td>			
			</tr>
		</table>
		<form>
	</body>
</html>
